#!/usr/bin/python
# encoding:utf-8

# ****************************************************************
# Description： 入口文件，实际项目中只需要修改 ExcelPath和SheetName即可
# Author： jf
# Date：20170605
# version：
# ****************************************************************

from server_case import *
import server_case
import mod_config
import cmd


# 群发邮件列表：按照如下格式填写地址即可，邮件使用","(英文逗号)分开
# ["jiangfei@longsung.com","zhangshuai@longsung.com"]
# mail_list=["jiangfei@longsung.com","zhangshuai@longsung.com"]

def environment_check():
	# mail_list = ATcmdtest.mod_config.getConfig("CFG", "Mail_List")
	SheetName = mod_config.getConfig("CFG", "SheetName")
	ExcelPath = os.getcwd() + mod_config.getConfig("CFG", "Path_Excel")
	# print SheetName
	# print mail_list
	# print ExcelPath
	# 占用并打开当前APP串口
	com = ComList()

	if not com:
		mod_config.setConfig("CFG", "casestate", False)
		print (u'没有找到APP端口，请检查模块上电或开发板连接后重新运行！')
		return None, None

	if "NONE" == SheetName.upper() or "" == SheetName:
		mod_config.setConfig("CFG", "casestate", False)
		print u'请选择需要测试的页面！'
		return None, None

	if 'ALL' == SheetName.upper():
		Sheetnames = []
		# 执行所有sheet页的用例
		try:
			server_case.excelobj = create_excel(ExcelPath, SheetName)
			Sheetnames = list(server_case.excelobj.get_all_sheetname())
		except:
			print(u'打开文件异常，请检查...')
			server_case.excelobj.close()

	else:
		# 执行指定sheet页的用例
		# mailbody, count = case_run(str(SheetName), comapp)
		# Sendmail(mail_list, mailbody, count, SheetName)
		Sheetnames = list(SheetName.split(",", 1))
		try:
			server_case.excelobj = create_excel(ExcelPath, SheetName)
		except:
			print(u'打开文件异常，请检查...')
			server_case.excelobj.close()
		# print Sheetnames
		SheetnamesfromExcel = list(server_case.excelobj.get_all_sheetname())
		# print Sheetnames
		# print SheetnamesfromExcel
		for SheetName in Sheetnames:
			if SheetName.lower() not in SheetnamesfromExcel and SheetName.upper() not in SheetnamesfromExcel:
				mod_config.setConfig("CFG", "casestate", False)
				print u'测试用例选择错误'
				server_case.excelobj.close()
				return None, None

	comapp = openport(com[0])

	return Sheetnames, comapp


def case_begin():
	# 获取当前运行状态，已运行则直接返回
	casestate = mod_config.getConfig("CFG", "casestate")
	# print casestate
	# print type(casestate)
	if casestate == 'True':
		return

	# 检查选择的sheet页面和串口是否正常
	Sheetnames, comapp = environment_check()

	# 获取收件人地址
	mail_list = mod_config.getConfig("CFG", "Mail_List")

	# SheetName = ATcmdtest.mod_config.getConfig("CFG", "SheetName")
	# ExcelPath = os.getcwd() + ATcmdtest.mod_config.getConfig("CFG", "Path_Excel")
	# # print SheetName
	# # print mail_list
	# # print ExcelPath
	# ATcmdtest.server_case.excelobj = create_excel(ExcelPath, SheetName)
	#
	# # 占用并打开当前APP串口
	# com = ComList()
	# if com:
	# 	comapp = openport(com[0])
	# else:
	# 	print '没有找到APP端口，请检查模块是否上电或连接正常'
	# 	return
	#
	# #清除模块和SIM卡中的原有配置信息，以及模块，短信等
	# init = Init(comapp)
	# init.init_system()
	#
	# # 跑case,执行结束发出邮件通知\
	# if SheetName.upper() == "NONE" or  SheetName == "":
	# 	print '请选择需要测试的页面！'
	# 	return
	#
	# elif SheetName.upper() == 'ALL':
	# 	# 执行所有sheet页的用例
	# 	# Sheetnames = []
	# 	Sheetnames = list(ATcmdtest.server_case.excelobj.get_all_sheetname())
	#
	# else:
	# 	# 执行指定sheet页的用例
	# 	# mailbody, count = case_run(str(SheetName), comapp)
	# 	# Sendmail(mail_list, mailbody, count, SheetName)
	# 	Sheetnames = list(SheetName.split(",", 1))
	# 	# print Sheetnames
	# 	SheetnamesfromExcel = list(ATcmdtest.server_case.excelobj.get_all_sheetname())
	# 	# print Sheetnames
	# 	# print SheetnamesfromExcel
	# 	for SheetName in Sheetnames:
	# 		if SheetName.lower() not in SheetnamesfromExcel:
	# 			print '测试用例选择错误'
	# 			return

	if Sheetnames is None or comapp is None:
		print(u'\n环境异常，请检查环境后重新运行')
		raw_input('Press Enter to exit...')
		return
	try:
		for SheetName in Sheetnames:
			SheetName = str(SheetName.upper())

			# 清除模块和SIM卡中的原有配置信息，以及模块，短信等
			# print SheetName
			init = Init(comapp, SheetName.upper())
			init.init_system()

			count = case_run(SheetName, comapp)
			Sendmail(mail_list, count, SheetName)
	except:
		print(u'\n测试异常，请检查测试用例及环境后重新运行')
		raw_input('Press Enter to exit...')
	finally:
		# 跑完case后，打包另存一份excel
		# 跑完case后，关闭excel
		server_case.excelobj.saveAs()
		server_case.excelobj.close()

		# 获取当前运行状态，已运行则直接返回
		mod_config.setConfig("CFG", "casestate", False)
		raw_input('Press Enter to exit...')


def main():
	ret = cmd.menu()
	if ret == 'R':
		case_begin()
	elif ret == 'Q':
		return


if __name__ == "__main__":
	cmd.title()
	main()
# 保证cmd窗口运行完后不退出
# raw_input('Press Enter to exit...')
